package com.example.peibei.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dingtao.common.bean.HomeCateTwo;
import com.dingtao.common.bean.Order;
import com.dingtao.common.bean.User;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.ui.activity.CateOrderActivity;
import com.example.peibei.ui.activity.LoginActivity;
import com.example.peibei.ui.activity.MoreRecommendActivity;
import com.example.peibei.ui.activity.OrderDetailActivity;
import com.example.peibei.ui.activity.SearchActivity;
import com.example.peibei.ui.activity.WebviewActivity;
import com.example.peibei.ui.adapter.HomeCateAdapter;
import com.example.peibei.ui.adapter.RecommendMemberAdapter;
import com.example.peibei.ui.weight.HorizontalPageLayoutManager;
import com.example.peibei.ui.weight.PagingScrollHelper;
import com.facebook.drawee.view.SimpleDraweeView;
import com.tencent.mmkv.MMKV;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;

import java.util.ArrayList;
import java.util.List;

import me.relex.circleindicator.CircleIndicator;


public class HomeChild2Head {
    private Context context;
    private View view;
    private View rl_recommend;
    private View rl_local;
    private RecyclerView rv_tuijian;
    private RecyclerView rv_cate_one;
    private MZBannerView mMZBanner;
    private CircleIndicator circleindicator2;
    private HomeCateAdapter homeCateAdapterOne;
    private RecommendMemberAdapter recommendMemberAdapter;
    private List<HomeCateTwo.BannerDTO> bannerData;

    public HomeChild2Head(Context context) {
        this.context = context;
        findView();
        initView();
    }

    public View getView() {
        return view;
    }

    private void findView(){
        view = LayoutInflater.from(context).inflate(R.layout.head_home_child2,null,false);
        rl_recommend = view.findViewById(R.id.rl_recommend);
        rv_tuijian = view.findViewById(R.id.rv_tuijian);
        rv_cate_one = view.findViewById(R.id.rv_cate_one);
        circleindicator2 = view.findViewById(R.id.circleindicator2);
        mMZBanner = view.findViewById(R.id.banner);
        rl_local = view.findViewById(R.id.rl_local);

        view.findViewById(R.id.iv_more).setOnClickListener(recommendClick);
        view.findViewById(R.id.tv_more).setOnClickListener(recommendClick);
        view.findViewById(R.id.rl_search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it  =new Intent(context,SearchActivity.class);
                context.startActivity(it);
            }
        });
    }

    private void initView(){
        recommendMemberAdapter = new RecommendMemberAdapter();
        recommendMemberAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                User loginUser = SpConstant.getLoginUser();
                if (loginUser.getId() == 0){
                    Intent it = new Intent(context,LoginActivity.class);
                    context.startActivity(it);
                    return;
                }

                Order.RecordsDTO recordsDTO = recommendMemberAdapter.getItem(position);
                String publishOrderId = recordsDTO.getPublishOrderId();
                Bundle bundle = new Bundle();
                bundle.putString("publishOrderId", publishOrderId);
                Intent it = new Intent(context,OrderDetailActivity.class);
                it.putExtras(bundle);
                context.startActivity(it);
            }
        });
        rv_tuijian.setLayoutManager(new LinearLayoutManager(context, RecyclerView.HORIZONTAL, false));
        rv_tuijian.setAdapter(recommendMemberAdapter);

        homeCateAdapterOne = new HomeCateAdapter();
        homeCateAdapterOne.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                HomeCateTwo.CategoryDTO data = homeCateAdapterOne.getItem(position);
                Bundle bundle = new Bundle();
                bundle.putString("caption", data.getCaption());
                bundle.putInt("id", data.getId());
                Intent it = new Intent(context,CateOrderActivity.class);
                it.putExtras(bundle);
                context.startActivity(it);
            }
        });
        HorizontalPageLayoutManager horizontalPageLayoutManager = new HorizontalPageLayoutManager(2, 5);//这里两个参数是行列，这里实现的是一行三列
        rv_cate_one.setLayoutManager(horizontalPageLayoutManager);
        rv_cate_one.setHorizontalScrollBarEnabled(true);
        rv_cate_one.setAdapter(homeCateAdapterOne);
        //使用通用RecyclerView组件
        //初始化横向管理器
        PagingScrollHelper scrollHelper = new PagingScrollHelper();
        scrollHelper.setUpRecycleView(rv_cate_one);//将横向布局管理器和recycler view绑定到一起
        scrollHelper.setOnPageChangeListener(new PagingScrollHelper.onPageChangeListener() {
            @Override
            public void onPageChange(int index) {
                circleindicator2.animatePageSelected(index);
            }
        });
        scrollHelper.updateLayoutManger();
        scrollHelper.scrollToPosition(0);//默认滑动到第一页

        bannerData = new ArrayList<>();
        mMZBanner.setBannerPageClickListener(new MZBannerView.BannerPageClickListener() {
            @Override
            public void onPageClick(View view, int position) {
                String htmlUrl = bannerData.get(position).getHtmlUrl();
                Bundle bundle = new Bundle();
                bundle.putString("htmlUrl", htmlUrl);
                Intent it =new Intent(context,WebviewActivity.class);
                it.putExtras(bundle);
                context.startActivity(it);
            }
        });
        mMZBanner.setIndicatorVisible(false);
    }

    //显示隐藏超级服务
    public void showAndHideCZServe(){
        boolean showPush = MMKV.defaultMMKV().decodeBool("orientation_push_new", false);
        if (showPush){
            rl_recommend.setVisibility(View.VISIBLE);
            rv_tuijian.setVisibility(View.VISIBLE);
        }else{
            rl_recommend.setVisibility(View.GONE);
            rv_tuijian.setVisibility(View.GONE);
        }
    }

    public void setRecommendMemberData(List<Order.RecordsDTO> list, boolean isRefresh){
        if (isRefresh){
            recommendMemberAdapter.setNewInstance(list);
        }else {
            recommendMemberAdapter.addData(list);
        }
    }

    public void setHomeCateData(List<HomeCateTwo.CategoryDTO> list, boolean isRefresh){
        if (isRefresh){
            homeCateAdapterOne.setNewInstance(list);
        }else {
            homeCateAdapterOne.addData(list);
        }
        resetData(homeCateAdapterOne.getItemCount());
    }

    private void resetData(int max) {
        //计算较大的数组长度
        int i = max / 10;
        int j = max % 10;
        if (j == 0) {
            circleindicator2.createIndicators(i, 0);
        } else {
            circleindicator2.createIndicators(i + 1, 0);
        }
    }

    public void showLocal(boolean isShow){
        if (isShow) {
            rl_local.setVisibility(View.VISIBLE);
        } else {
            rl_local.setVisibility(View.GONE);
        }
    }

    public void setBannerData(List<HomeCateTwo.BannerDTO> list){
        this.bannerData = list;
        if (list != null){
            for (HomeCateTwo.BannerDTO bannerDTO : list) {
                System.out.println("轮播图："+bannerDTO.getImageUrl());
            }
        }
        mMZBanner.setPages(bannerData, (MZHolderCreator<BannerViewHolder>) BannerViewHolder::new);
        mMZBanner.start();
    }

    class BannerViewHolder implements MZViewHolder<HomeCateTwo.BannerDTO> {
        private ImageView mImageView;

        @Override
        public View createView(Context context) {
            // 返回页面布局
            View view = LayoutInflater.from(context).inflate(R.layout.banner_item, null);
            mImageView = view.findViewById(R.id.banner_image);
            return view;
        }

        @Override
        public void onBind(Context context, int position, HomeCateTwo.BannerDTO data) {
            Glide.with(context).load(data.getImageUrl()).into(mImageView);
        }
    }

    private View.OnClickListener recommendClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            moreRecommend();
        }
    };
    private void moreRecommend() {
        User loginUser = SpConstant.getLoginUser();
        if (loginUser.getId() == 0){
            Intent it = new Intent(context,LoginActivity.class);
            context.startActivity(it);
            return;
        }
        Intent it = new Intent(context,MoreRecommendActivity.class);
        context.startActivity(it);
    }
}
